package com.zy.controller;

import com.zy.mall.dao.UserDAO;
import com.zy.mall.entity.User;
import com.zy.mall.po.ResStatus;
import com.zy.mall.service.UserService;
import com.zy.mall.po.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/user")
@Api(value = "实现用户相关操作", tags = "用户信息控制器")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserDAO userDAO;

    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "username", value = "用户登录账号", required = true),
            @ApiImplicitParam(dataType = "string", name = "password", value = "用户登录密码", required = true)
    })
    @GetMapping("/login")
    public ResultVo login(@RequestParam("username") String name,
                          @RequestParam(value = "password") String pwd){
        ResultVo resultVo = userService.checkLogin(name, pwd);
        return resultVo;
    }

    @ApiOperation("用户注册接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "username", value = "用户注册账号", required = true),
            @ApiImplicitParam(dataType = "string", name = "password", value = "用户注册密码", required = true)
    })
    @PostMapping("/regist")
    public ResultVo regist(@RequestBody User user){
        return userService.userRegist(user.getUsername(), user.getPassword());
    }

    @ApiOperation("校验token是否过期接口")
    @GetMapping("/check")
    public ResultVo userTokencheck(@RequestHeader("token") String token){
        return new ResultVo(ResStatus.OK,"success",null);
    }

    @ApiIgnore
    @RequestMapping("/test")
    public User login01(String name, String pwd){
        return userDAO.queryUserByName(name);
    }

}
